package com.ceres.customer.task;

import com.ceres.customer.dao.CustMapper;
import com.ceres.customer.dao.CustUserMapper;
import com.ceres.customer.pojo.Customer;
import com.ceres.customer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 定时任务
 */
@Component
public class CustomerTask {

    @Autowired
    private CustMapper custMapper;

    private CustUserMapper custUserMapper;

    /**
     * 定时查询数据库
     * 每天0点执行
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    public void custTask() {
        //查询所有农业组织信息
        List<Customer> customers = custMapper.selectAll();
        for (Customer customer : customers) {
            //获取每个农业组织的到期时间
            Date endTime = customer.getEndTime();
            //判断到期时间与当前时间
            if (new Date().getTime() > endTime.getTime()) {
                //如果当前时间大于到期时间,则将农场组织状态改为停用0
                //同时将该农业组织下用户的状态改为一致
                customer.setStatus("0");
                custMapper.updateByPrimaryKey(customer);
            }
        }
    }
}
